package main

import (
	"fmt"
	"reflect"
	"time"
)

// 接口定义
/* -------------------------------------------------------------------------------------------------------------------- */

// 连接器接口定义了连接方法
type Connector interface {
	Connect()
}

// USB接口定义了获取设备名称方法, 并且内嵌连接器接口
type USB interface {
	Name() string
	Connector
}

// 手机连接器
/* -------------------------------------------------------------------------------------------------------------------- */

// 手机连接器实现USB接口
type PhoneConnector struct {
	name string
}

func (P *PhoneConnector) Name() string {
	return P.name
}

func (P *PhoneConnector) Connect() {
	fmt.Println("Phone : " + P.name + " Connect To PC !")
}

// 声卡连接器
/* -------------------------------------------------------------------------------------------------------------------- */

// 声卡连接器实现USB接口
type SoundCardConnector struct {
	name      string
	byteRange map[string]uint16
}

func (S *SoundCardConnector) Name() string {
	return S.name
}

func (S *SoundCardConnector) Connect() {
	fmt.Println("SoundCard : " + S.name + " Connect To PC !")
}

// 除了接口定义的方法, 还可以有属于自己的方法
func (S *SoundCardConnector) ByteRange() {
	minByte, minOk := S.byteRange["min"]
	maxByte, maxOk := S.byteRange["max"]
	if !minOk || !maxOk {
		fmt.Println("SoundCard : Range : " + S.name + " Not Has ByteRange Information !")
		return
	}
	fmt.Printf("SoundCard : Range : %d-%d bit \n", minByte, maxByte)
}

// 主代码开始
/* -------------------------------------------------------------------------------------------------------------------- */

func main() {

	fmt.Println("USB Connect Event Log : ")
	fmt.Println("")
	fmt.Println(time.Now().Local())
	fmt.Println("-----------------------------------------")

	// Phone
	var MeiZuPro6 = &PhoneConnector{"MeiZuPro6"}
	usb := (USB)(MeiZuPro6)
	usb.Connect()
	fmt.Println("DeviceName : " + usb.Name())
	fmt.Println(reflect.TypeOf(usb))

	fmt.Println("")
	fmt.Println(time.Now().Local())
	fmt.Println("-----------------------------------------")

	// SoundCard
	var Z6 = &SoundCardConnector{"Z6 HiFi Player", map[string]uint16{"min": 32, "max": 256}}
	soundUsb := (USB)(Z6)		// 因为实现了USB接口, 所以可以强转为USB类型
	soundUsb.Connect()
	fmt.Println("DeviceName : " + soundUsb.Name())
	fmt.Println(reflect.TypeOf(soundUsb))
	Z6.ByteRange()

	fmt.Println("")
	fmt.Println(time.Now().Local())
	fmt.Println("-----------------------------------------")
}
